package com.boarsoft.boar.sequence.biz.impl;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.sequence.biz.SequenceInfoBiz;
import com.boarsoft.boar.sequence.entity.SequenceInfo;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("sequenceBiz")
public class SequenceInfoBizImpl extends SimpleBizImpl implements SequenceInfoBiz {

	@Override
	@Transactional(readOnly = true)
	public PagedResult<SequenceInfo> list(String catalog, String key, String orderBy, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		if (Util.strIsNotEmpty(catalog)) {
			sb.append(" and catalog='").append(catalog).append("'");
		}
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (code like '%").append(key);
			sb.append("%' or name like '%").append(key);
			sb.append("%')");
		}
		int total = dao.getTotal(SequenceInfo.class, sb.toString());
		if (Util.strIsNotEmpty(orderBy)) {
			sb.append(" order by ").append(orderBy);
		}
		List<SequenceInfo> lt = dao.list(SequenceInfo.class, sb.toString());
		return new PagedResult<SequenceInfo>(total, lt, pageNo, pageSize);
	}

	@Override
	@Transactional(readOnly = true)
	public SequenceInfo find(String catalog, String code) {
		StringBuilder sb = new StringBuilder();
		sb.append("and catalog = '").append(catalog);
		sb.append("' and code = '").append(code).append("'");
		return dao.find(SequenceInfo.class, sb.toString());
	}

	@Override
	@Transactional(readOnly = true)
	public SequenceInfo get(String id) {
		return dao.get(SequenceInfo.class, id);
	}

	@Override
	@Transactional
	public void save(SequenceInfo o) {
		if (Util.strIsEmpty(o.getId())) {
			dao.save(o);
		} else {
			dao.update(o);
		}
	}

	@Override
	@Transactional
	public void delete(String id) {
		dao.delete(SequenceInfo.class, id);
	}
}
